#ifndef MOD_BUS_H_
#define MOD_BUS_H_

#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define MD_WRITE_D (0)
#define MD_READ_D (1)
#define MD_COIL_G (2)
#define MD_COIL_S (3)

#define MAX_RW_REG_NUM (10)

typedef struct {
	uint8_t  rw_bit;  //0 write, 1 read;
	uint8_t  dev_id;
	uint16_t reg_id;
	uint16_t rwlen;
	int16_t *wbuf;
	uint32_t *reslen;
	uint8_t  *resbuf;
} md_dev_t;

int32_t modbus_init(void);
int modbus_opt(md_dev_t *md_buf);
int16_t get_reg(uint16_t addr, uint16_t reg);
int16_t set_reg(uint16_t addr, uint16_t reg, int16_t val);
int modbus_status(void);
int modbus_write(uint16_t addr, uint16_t reg, uint16_t writelen, uint16_t *wtbuf, uint8_t *buf, uint32_t *reslen);
int modbus_read(uint16_t addr, uint16_t reg, uint16_t writelen, uint8_t *buf, uint32_t *reslen);
int modbus_get(uint16_t addr, uint16_t reg, uint16_t writelen, uint8_t *buf, uint32_t *reslen);
int modbus_set(uint16_t addr, uint16_t reg, uint16_t val, uint8_t *buf, uint32_t *reslen);

#ifdef __cplusplus
}
#endif

#endif /* MOD_BUS_H_ */


